package com.dacool.model;

import java.io.Serializable;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

import com.dacool.http.GetCoolMoneyRequest;
import com.dacool.util.FileCacheUtil;
import com.dacool.util.ViewUtils;

public class DCUser implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2379099064295538041L;

	private String name;
	private String password;
	private String userId;

	public DCAddress address = null;

	public boolean isValidUser() {
		return userId != null;
	}

	private DCUser() {

	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	transient private static DCUser instance;

	public static DCUser sharedUser() {
		if (instance == null) {
			instance = new DCUser();
		}
		return instance;
	}

	public void save() {
		FileCacheUtil
				.writeObject(ViewUtils.getContext(), instance, "savedUser");
	}

	public void load() {
		DCUser user = (DCUser) FileCacheUtil.readObject(ViewUtils.getContext(),
				"savedUser");
		if (user != null) {
			instance.name = user.name;
			instance.password = user.password;
			instance.userId = user.userId;
			instance.address = user.address;
		}
		new GetCoolMoneyRequest().start();
	}
}
